﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.WindowsCE.Forms;

namespace MobileCapture
{
    public partial class frmMain : Form
    {
        const int SRCCOPY = 0x00CC0020;

        public frmMain()
        {
            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.WindowState = FormWindowState.Normal;
            this.ControlBox = true;
            this.MinimizeBox = true;
            this.MaximizeBox = true;

            panelCapture.Height = 228;
            panelActivation.Height = 20;
            panelOptions.Height = 20;
        }

        // P/Invoke declaration
        [DllImport("coredll.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

        const int SW_MINIMIZED = 6;

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        private void Snapshot(string filename, Graphics gx, Rectangle rect)
        {
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            // Create compatible graphics
            Graphics gxComp = Graphics.FromImage(bmp);
            // Blit the image data
            BitBlt(gxComp.GetHdc(), 0, 0, rect.Width, rect.Height, gx.GetHdc(), rect.Left, rect.Top, SRCCOPY);
            bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
            // Cleanup
            bmp.Dispose();
            gxComp.Dispose();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            comboBoxHardwareKey.SelectedIndex = 0;
        }

        private void menuItemCapture_Click(object sender, EventArgs e)
        {
            // commented out due to didn't remove menu from botton of screen in time for screen capture

            // this.Visible = false;
            // this.Refresh();

            // this code minimizes the form
            ShowWindow(this.Handle, SW_MINIMIZED);

            if (radioButtonDelayTimer.Checked == true)
            {
                for (int i = 0; i < numericUpDownCaptureCount.Value; i++)
                {
                    Thread.Sleep((int)numericUpDownSeconds.Value * 1000);

                    int x1 = textBoxX1.IntValue;
                    int y1 = textBoxY1.IntValue;
                    int width = textBoxX2.IntValue - x1;
                    int height = textBoxY2.IntValue - y1;

                    string fileName = string.Format("\\screenshot{0}.bmp", (i + 1).ToString("000"));

                    Snapshot(fileName, this.CreateGraphics(), new Rectangle(x1, y1, width, height));
                }

                this.Close();
            }
        }

        private int captureNowCount = 0;

        private void buttonCaptureNow_Click(object sender, EventArgs e)
        {
            int x1 = textBoxX1.IntValue;
            int y1 = textBoxY1.IntValue;
            int width = textBoxX2.IntValue - x1;
            int height = textBoxY2.IntValue - y1;

            string fileName = string.Format("\\capturenow{0}.bmp", (captureNowCount++).ToString("000"));

            Snapshot(fileName, this.CreateGraphics(), new Rectangle(x1, y1, width, height));

            this.WindowState = FormWindowState.Normal;
        }

        private void buttonCapture_Click(object sender, EventArgs e)
        {
            panelCapture.Height = 228;
            panelActivation.Height = 20;
            panelOptions.Height = 20;
        }

        private void buttonActivation_Click(object sender, EventArgs e)
        {
            panelCapture.Height = 20;
            panelActivation.Height = 228;
            panelOptions.Height = 20;
        }

        private void buttonOptions_Click(object sender, EventArgs e)
        {
            panelCapture.Height = 20;
            panelActivation.Height = 20;
            panelOptions.Height = 228;
        }

        private void checkBoxMultipleCaptures_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxMultipleCaptures.Checked == true)
                numericUpDownCaptureCount.Text = "2";
            else
                numericUpDownCaptureCount.Text = "1";
        }

        private void radioButtonFullScreen_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonFullScreen.Checked == true)
            {
                // radioButtonArea.Checked = false;
                textBoxX1.ReadOnly = true;
                textBoxY1.ReadOnly = true;
                textBoxX2.ReadOnly = true;
                textBoxY2.ReadOnly = true;

                textBoxX1.Text = "0";
                textBoxY1.Text = "0";
                textBoxX2.Text = "240";
                textBoxY2.Text = "320";
            }
        }

        private void radioButtonArea_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonArea.Checked == true)
            {
                // radioButtonFullScreen.Checked = false;
                textBoxX1.ReadOnly = false;
                textBoxY1.ReadOnly = false;
                textBoxX2.ReadOnly = false;
                textBoxY2.ReadOnly = false;
            }

        }

        private void comboBoxHardwareKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindHardwareKey();
        }

        private void radioButtonHardwareKey_CheckedChanged(object sender, EventArgs e)
        {
            BindHardwareKey();
        }

        private void BindHardwareKey()
        {
            switch (comboBoxHardwareKey.Text)
            {
                case "ApplicationKey1":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey1;
                    break;
                case "ApplicationKey2":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey2;
                    break;
                case "ApplicationKey3":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey3;
                    break;
                case "ApplicationKey4":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey4;
                    break;
                case "ApplicationKey5":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey5;
                    break;
                case "ApplicationKey6":
                    hardwareButton.HardwareKey = HardwareKeys.ApplicationKey6;
                    break;
            }
        }
    }
}